<template>
  <div style="text-align: center">
    <h2>修改密码</h2>

    <el-form
      :model="passForm"
      status-icon
      :rules="rules"
      ref="passForm"
      label-width="100px"
    >
      <el-form-item label="旧密码" prop="currentPass">
        <el-input
          type="password"
          v-model="passForm.currentPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input
          type="password"
          v-model="passForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="passForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('passForm')"
          >提交</el-button
        >
        <!-- <el-button @click="resetForm('passForm')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.passForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userInfo: {},
      passForm: {
        password: "", //新
        checkPass: "", //确认
        currentPass: "", //旧
      },
      rules: {
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 8,max: 18,message: "长度在 8 到 18 个字符",trigger: "blur", },
          { pattern: /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/, message: '新密码需包含数字，大小写字母',trigger: "blur" }
        ],
        checkPass: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        currentPass: [
          { required: true, message: "请输入当前密码", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            id: JSON.parse(localStorage.getItem("user")).user_id,
            pass: this.passForm.currentPass,
            new_pass: this.passForm.password,
          };
          // /company/index/edit_pass
          this.$axios.post("admin/index/edit_pass", data).then((res) => {
            this.$alert("修改成功，需重新登录", "提示", {
              confirmButtonText: "确定",
              callback: (res2) => {
                console.log(res2);
                this.$refs[formName].resetFields();

                localStorage.clear();
                sessionStorage.clear();
                this.$store.commit("resetState");
                // this.$message.success("退出成功");
                this.$router.push("/login");
              },
            });
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.el-form {
  width: 420px;
  margin: 50px auto;
}
</style>